package com.ztjy.purchase.biz.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztjy.bean.CDetails;
import com.ztjy.bean.CPurchase;
import com.ztjy.purchase.biz.CDetailsBiz;
import com.ztjy.purchase.biz.CPurchaseBiz;
import com.ztjy.purchase.dao.CPurchaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class CPurchaseBizImpl implements CPurchaseBiz {
    @Autowired
    private CPurchaseDao pudao;
    @Autowired
    private CDetailsBiz dbiz;
    @Override
    public PageInfo<CPurchase> findCPurchaseByCount(Integer pageNo, Integer pageSize, CPurchase cPurchase,
                                                    Date mintime, Date maxtime, Double mintotal, Double maxtotal) {
        PageHelper.startPage(pageNo,pageSize);
        List<CPurchase> cPurchases = pudao.selectCPurchaseByCount(cPurchase, mintime, maxtime, mintotal, maxtotal);
        PageInfo<CPurchase> pageInfo = new PageInfo<CPurchase>(cPurchases);
        return pageInfo;
    }

    @Override
    public PageInfo<CPurchase> findCPurchaseByCountStatus2(Integer pageNo, Integer pageSize, CPurchase cPurchase, Date mintime, Date maxtime, Double mintotal, Double maxtotal) {
        PageHelper.startPage(pageNo,pageSize);
        cPurchase.setCheckStatus(2);
        List<CPurchase> cPurchases = pudao.selectCPurchaseByCount(cPurchase, mintime, maxtime, mintotal, maxtotal);
        PageInfo<CPurchase> pageInfo = new PageInfo<CPurchase>(cPurchases);
        return pageInfo;
    }

    @Override
    public List<CPurchase> findAllCPurchase(CPurchase purchase) {
        return pudao.findAllCPurchase(purchase);
    }


    @Override
    public CPurchase findCPurchaseByPid(String purchaseId) {
        return pudao.selectByPrimaryKey(purchaseId);
    }

    @Override
    public List<CPurchase> findListCPurchaseByPid(String purchaseId) {
        return pudao.findListByIdCPurchase(purchaseId);
    }

    @Override
    public boolean addCPurchase(CPurchase cPurchase, CDetails cDetails) {
        boolean b = dbiz.addCDetails(cDetails);
        boolean flag=false;
        try {
            flag=pudao.insertSelective(cPurchase);
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean updateCPurchase(CPurchase cPurchase) {
        boolean flag=false;
        try {
            flag=pudao.updateByPrimaryKeySelective(cPurchase);
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean delCPurchase(String purchaseId) {
        boolean flag=false;
        try {
            flag=pudao.deleteByPrimaryKey(purchaseId);
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean updateStatus(String purchaseId, Integer checkId) {
        CPurchase cPurchase = pudao.selectByPrimaryKey(purchaseId);
        cPurchase.setCheckStatus(2);
        cPurchase.setCheckId(checkId);
        Date date=new Date(System.currentTimeMillis());
        cPurchase.setCheckTime(date);
        boolean flag=false;
        try {
            flag= pudao.updateByPrimaryKeySelective(cPurchase);
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public List<CPurchase> findNoCK(CPurchase purchase) {
        return pudao.findNoCK();
    }
}
